////////////////////////////////////////////////////////////////////////////////
//
//  Pixelami.com
//  Copyright 2011 Original Authors (Alexander Syed et al)
//  All Rights Reserved.
//
//  NOTICE: Pixelami permits you to use, modify, and distribute this file
//  in accordance with the terms of the license agreement accompanying it.
//
////////////////////////////////////////////////////////////////////////////////

package org.pixelami.hercularity.descriptors
{
	
	
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	
	import org.pixelami.utils.ClassUtils;
	
	public class DeferredAction extends Action
	{
		/**
		 * The Class reference of a task Class
		 */
		public var actionClass:Class;
		
		/**
		 * The arguments that will be passed to actionClass when it is instantiated
		 */
		public var constructorArgs:Array;
		
		public function DeferredAction(actionClass:Class, constructorArgs:Array=null, breakOnFailure:Boolean=false, 
									   successEventType:String=Event.COMPLETE, failureEventType:String=ErrorEvent.ERROR, 
									   startMethodName:String="start")
		{
			super(null, breakOnFailure, successEventType, failureEventType, startMethodName);
			
			this.actionClass = actionClass;
			this.constructorArgs = constructorArgs;
		}
		
		/**
		 * creates <code>target</code> lazily by instantiating actionClass
		 */
		
		override public function get target():IEventDispatcher
		{
			if(!super.target)
			{
				super.target = ClassUtils.constructWithArguments(actionClass,constructorArgs);
			}
			return super.target;
		}
		
		
	}
}